<!-- 首页 -->
<template>
	<view>
		<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" :height="height" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
		    <view class="user-box" v-for="(item, index) in dataList" :key="index">
		    	<u-avatar @click="chat(item)" size="large" :src="item.avatar"></u-avatar>
		    	<view class="user-box-right">
		    		<view>
		    			<view class="user-name-box">
		    				<view class="nickname">{{item.nickName}}</view>
		    				<view class="sex-box-nv" v-if="item.sex == 1">
		    					<u-icon name="ziyuan2" custom-prefix="iconfont"></u-icon>
		    					<view class="age-text">{{item.age}}</view>
		    				</view>
		    				<view class="sex-box" v-else>
		    					<u-icon name="ziyuan3" custom-prefix="iconfont"></u-icon>
		    					<view class="age-text">{{item.age}}</view>
		    				</view>
							<view class="vip-box" v-if="item.isPay == '1'">
								<u-icon name="xianxinghuiyuan" custom-prefix="iconfont"></u-icon>
								<view class="vip-text">贵{{item.coinLevel}}</view>
							</view>
		    			</view>
		    			<view class="user-tag-box">
		    				<view class="user-tag">{{item.city}}</view>
		    				<view class="line"></view>
		    				<view class="user-tag">{{item.distance}}km</view>
		    			</view>
		    			<view v-if="item.signName == '' || item.signName == null ">{{item.signName}}</view>
		    			<view class="sign-name" v-else>{{item.signName}}</view>
		    		</view>
		    		<view>
						<u-button type="success" size="mini" @click="addWechat(item)" v-if="item.wechatFlag">加微信</u-button>
						<u-button type="primary" size="mini" @click="apply(item)" v-else>打招呼</u-button>
		    		</view>
		    	</view>
		    </view>
		</mescroll-uni>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js";
export default {
	mixins: [MescrollMixin,MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
	props:{
		i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
		index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			type: Number,
			default(){
				return 0
			}
		},
		tabs: { // 为了请求数据,演示用,可根据自己的项目判断是否要传
			type: Array,
			default(){
				return []
			}
		},
		height: [Number,String] ,// mescroll的高度
	    keyword: {
	        type: String,
	        default(){
	        	return ''
	        }
	    }
	},
	data() {
		return {
			downOption:{
				auto:false // 不自动加载 (mixin已处理第一个tab触发downCallback)
			},
			upOption:{
				auto:false, // 不自动加载
				page: {
					num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					size: 20 // 每页数据的数量
				},
				noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
				empty:{
			        icon: '/static/mescroll-empty.png',
					tip: '~ 暂无数据 ~', // 提示
					// btnText: '去看看'
				},
			    textNoMore:'没有更多了'
			},
			dataList: [],
		};
	},
	methods: {
		/*下拉刷新的回调 */
		downCallback() {
			// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
			// loadSwiper();
			// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			this.mescroll.resetUpScroll()
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) { 
			this.$u.get('/system/user/getListByVip',{
				pageNum: page.num,
				pageSize: page.size,
				lon: this.vuex_user.lon,
				lat: this.vuex_user.lat,
			}).then(res => {
				//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
				this.mescroll.endSuccess(res.rows.length);
				//设置列表数据
				if(page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
				this.dataList=this.dataList.concat(res.rows); //追加新数据
			}).catch(err => {
		        //联网失败, 结束加载
		        this.mescroll.endErr();
		    })
		},
		//点击空布局按钮的回调
		emptyClick(){
			uni.showToast({
				title:'点击了按钮,具体逻辑自行实现'
			})
		},
		addWechat(item) {
			this.judgeVipNum((res) => {
				if(res.wechatNum > 0){
					this.addVipApply(item);
				}else{
					this.addApply(item);
				}
			});
		},
		getAmount() {
			this.$u.get('/pay/userCoin/getAmount').then(res => {
				this.$u.vuex('vuex_amount', res.data);
			});
		},
		addVipApply(e) {
			this.$u.post('/wq/friendApply/addVipWechat',{
				userId: e.userId,
			}).then(res => {
				this.$refs.uToast.show({
					title: '剩余'+res.wechatNum+'次',
					type: 'success',
					duration: 1000,
					url: 'packgeMine/pages/index/chat?applyId='+res.id
				});
			});
		},
		addApply(e) {
			this.$u.post('/wq/friendApply/addWechat',{
				userId: e.userId,
			}).then(res => {
				this.getAmount();
				this.$u.route({
					url: 'packgeMine/pages/index/chat',
					params: {
						applyId: res.data.id,
					}
				});
			});
		},
		share() {
			var _this = this;
			_this.$u.get('/system/user/getListByCity',{
				pageNum: 1,
				pageSize: 3,
			}).then(res => {
				if(res.rows.length < 1){
					uni.showModal({
					    title: '温馨提示',
					    content: '分享3个好友，可以开通同城功能',
						confirmText:"去分享",
						cancelText:"取消",
					    success: function (e) {
					        if (e.confirm) {
								_this.judgeLogin((res) => {
									_this.$u.route({
										url: 'pages/index/share',
										params: {
											shareUserId: res.user.userId,
											my: true,
										}
									});
								});
					        } else if (e.cancel) {
								
					        }
					    }
					});
				}
			});
		},
		chat(item) {
			this.judgeLogin(() => {
				this.$u.route({
					url: 'packgeMine/pages/center/index',
					params: {
						userId: item.userId,
					}
				});
			});
		},
		apply(e) {
			this.$u.post('/wq/friendApply/add',{
				userId: e.userId,
			}).then(res => {
				this.$u.toast('申请成功');
			});
		},
		subscribeMessage(tmplIds) {
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: tmplIds,
				success: function(res) {
					console.log(JSON.stringify(res));
				},
				fail: function() {
		
				},
				complete: (res) => {
					console.log(333,res)
				}
			});
		},
	}
};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	.user-box {
		display: flex;
		align-items: center;
		padding: 15px;
		padding-bottom: 0;
	}
	.user-name-box {
		display: flex;
		align-items: center;
	}
	.nickname {
		font-size: 28rpx;
		margin-right: 5px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		max-width: 100px;
	}
	.sex-box {
		background-color: #2b85e4;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.sex-box-nv {
		background-color: #fe709f;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.user-tag-box {
		display: flex;
		align-items: center;
	}
	.user-tag {
		font-size: 20rpx;
		color: #82848a;
		margin: 10rpx 0;
	}
	.user-box-right {
		margin-left: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex: 1;
	}
	.sign-name {
		font-size: 24rpx;
		color: #8d8d8d;
		text-overflow: ellipsis;
		width: 400rpx;
		overflow: hidden;
		white-space: nowrap;
	}
	.line {
		float:left;
		width: 1px;
		height: 16rpx; 
		background: #e5e5e5;
		margin: 0 5px;
	}
	.vip-box {
		color: #f7ce05;
		margin-left: 5px;
		font-size: 36rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.vip-box {
		color: #fff;
		margin-left: 5px;
		font-size: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f7ce05;
		padding: 4rpx 8rpx;
		border-radius: 20px;
	}
	.vip-text {
		font-size: 16rpx;
		line-height: 2px;
	}
</style>
